package com.ossjk.asset.system.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.ossjk.asset.base.servlet.BaseServlet;
import com.ossjk.asset.base.util.AjaxReturn;
import com.ossjk.asset.base.util.CommonUtil;
import com.ossjk.asset.base.util.Constant;
import com.ossjk.asset.system.dao.DeviceDao;
import com.ossjk.asset.system.dao.DeviceLogDao;
import com.ossjk.asset.system.dao.UserDao;
import com.ossjk.asset.system.pojo.Device;
import com.ossjk.asset.system.pojo.DeviceVo;
import com.ossjk.asset.system.pojo.User;
import com.ossjk.asset.system.pojo.devicetype;

public class DeviceLogServlet extends BaseServlet {

	private Logger logger = Logger.getLogger(DeviceLogServlet.class);
	private DeviceLogDao deviceLogDao = new DeviceLogDao();
	
	private Logger logger1 = Logger.getLogger(DeviceServlet.class);
	private DeviceDao deviceDao = new DeviceDao();
	
	private Logger logger2 = Logger.getLogger(UserServlet.class);
	private UserDao userDao = new UserDao();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
		String method = getMethod(req);
		if ("log".equals(method)) {
			log(req, resp);
		} else if ("toInsert".equals(method)) {
			toInsert(req, resp);
		} else if ("insert".equals(method)) {
			insert(req, resp);
		} else if ("toUpdate".equals(method)) {
			toUpdate(req, resp);
		} else if ("update".equals(method)) {
			update(req, resp);
		} else if ("delete".equals(method)) {
			delete(req, resp);
		} else if ("batchDelete".equals(method)) {
			batchDelete(req, resp);
		}
	}

	/**
	 * 登记
	 * 
	 * @param req
	 * @param resp
	 */
	
	public void log(HttpServletRequest req, HttpServletResponse resp) {
		try {
			
			 List<Device> list1 = deviceLogDao.selectAll();
			 List<DeviceVo> list = new ArrayList<DeviceVo>();
			 
			 for(Device device:list1) {
				 devicetype dt = deviceDao.selectById(device.getDtid());
				 DeviceVo dv = new DeviceVo();
				 dv.setDevice(device);
				 dv.setDeviceType(dt);
				 list.add(dv);
			 }
			req.setAttribute("records", list);
			forward(req, resp, "/WEB-INF/page/system/device/deviceLog.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}

	}

	/**
	 * 去增加
	 * 
	 * @param req
	 * @param resp
	 */
	public void toInsert(HttpServletRequest req, HttpServletResponse resp) {
		try {
			
			req.setAttribute("records", deviceDao.selectAll());
			req.setAttribute("records2", userDao.selectAll());
			forward(req, resp, "/WEB-INF/page/system/device/DeviceInsertAndEdit.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 去更新
	 * 
	 * @param req
	 * @param resp
	 */
	public void toUpdate(HttpServletRequest req, HttpServletResponse resp) {
		try {
			
			
			req.setAttribute("record", deviceLogDao.selectById(req.getParameter("id")));
			req.setAttribute("records", deviceDao.selectAll());
			req.setAttribute("record2", userDao.selectAll());
			forward(req, resp, "/WEB-INF/page/system/device/DeviceInsertAndEdit.jsp");
		} catch (Exception e) {
			logger.error("", e);
			redirect(req, resp, req.getContextPath() + "/system/index.do?method=toError");
		}
	}

	/**
	 * 增加
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void insert(HttpServletRequest req, HttpServletResponse resp) {
		try {
			Device device1 = getParamtoObject(Device.class, req);
			
			
			/*String str1 = req.getParameter("Str_dtid");
			System.out.println(str1);
			
			String[] s = str1.split("-");
			String sql1 = "select * from devicetype where brand="+ "\"" +s[0] + "\"" + " " +"and model="+ "\""  +s[1] + "\"";
			System.out.println(sql1);
			
			devicetype dt = deviceDao.selectOne(sql1, devicetype.class);
			System.out.println(dt+"ddddddddddddd");
			
			String str2 = req.getParameter("Str_buyer");
			System.out.println(str2);
			
			String sql2 = "select * from devicetype where name="+str2;
			User u = userDao.selectOne(sql2, User.class);*/
			
			
			int deviceType_id = Integer.parseInt(req.getParameter("deviceType_id"));
			int user_id = Integer.parseInt(req.getParameter("user_id"));
			
			
			String dated = req.getParameter("buydate");
			String cdate = CommonUtil.char2(req.getParameter("proddate"), "yyyy-MM-dd");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date buydate = sdf.parse(dated);
			Date proddate = sdf.parse(cdate);
			
			device1.setProddate(proddate);
			device1.setBuydate(buydate);
			device1.setDtid(deviceType_id);
			device1.setCrtm(CommonUtil.getDate());
			device1.setMdtm(CommonUtil.getDate());
			device1.setCreatetime(CommonUtil.getDate());
			device1.setBuyer(user_id);
			if(device1.getStatus()==0) {
				device1.setStatus(1);
			}
			if (deviceLogDao.insert(device1) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 更新
	 * 
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	public void update(HttpServletRequest req, HttpServletResponse resp) {
		try {
			Device device1 = getParamtoObject(Device.class, req);
			device1.setMdtm(CommonUtil.getDate());
			if (deviceLogDao.updateBySelected(device1) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 删除
	 * 
	 * @param req
	 * @param resp
	 */
	public void delete(HttpServletRequest req, HttpServletResponse resp) {
		try {
			System.out.println(req.getParameter("device_id"));
			if (deviceLogDao.delete(req.getParameter("device_id")) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

	/**
	 * 批量删除
	 * 
	 * @param req
	 * @param resp
	 */
	public void batchDelete(HttpServletRequest req, HttpServletResponse resp) {
		try {
			if (deviceLogDao.batchDelete(req.getParameterValues("ids")) > 0) {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_SUCCESS, Constant.RETURN_MESSAGE_SUCCESS));
			} else {
				renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_ERROR, Constant.RETURN_MESSAGE_ERROR));
			}
		} catch (Exception e) {
			logger.error("", e);
			renderAjaxReturn(resp, new AjaxReturn(Constant.RETURN_CODE_EXCEPTION, Constant.RETURN_MESSAGE_EXCEPTION));
		}
	}

}